package com.slkj.shilixiaoyuanapp.ui.main;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;

import java.io.File;
import java.text.NumberFormat;
import java.text.ParseException;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import zlc.season.rxdownload3.RxDownload;
import zlc.season.rxdownload3.core.Downloading;
import zlc.season.rxdownload3.core.Failed;
import zlc.season.rxdownload3.core.Normal;
import zlc.season.rxdownload3.core.Status;
import zlc.season.rxdownload3.core.Succeed;
import zlc.season.rxdownload3.core.Suspend;
import zlc.season.rxdownload3.extension.ApkInstallExtension;
import static zlc.season.rxdownload3.helper.UtilsKt.dispose;

@ActivityInfo(layout = R.layout.activity_update_progress,isShowActionBar = false)
public class UpdateProgressActivity extends BaseActivity {

    @BindView(R.id.progress)
    ProgressBar progressBar;
    @BindView(R.id.tv_progress)
    TextView tv_progress;
    private Disposable disposable;
    private Status currentStatus = new Status();
    @InjectParam
    String url;
    @BindView(R.id.ll_progress)
    RelativeLayout ll_progress;
    @BindView(R.id.iv_progress)
    ImageView iv_progress;

    @Override
    protected void init() {
        UpdateProgressActivity__JumpCenter.bind(this);
        create();
    }

    private void create() {
        disposable = RxDownload.INSTANCE.create(url, true)
                .observeOn(AndroidSchedulers.mainThread())
                .doOnComplete(() -> {

                })
                .subscribe(status -> {
                    currentStatus = status;
                    setProgress(status);
                        dispatchClick();
                });
    }

    private void dispatchClick() {
        if (currentStatus instanceof Normal) {
            start();
        } else if (currentStatus instanceof Suspend) {
            start();
        } else if (currentStatus instanceof Failed) {
            start();
        } else if (currentStatus instanceof Downloading) {
            stop();
        } else if (currentStatus instanceof Succeed) {
            install();
        } else if (currentStatus instanceof ApkInstallExtension.Installed) {
            open();
        }
    }

    private void setProgress(Status status) {
        progressBar.setMax((int) status.getTotalSize());
        progressBar.setProgress((int) status.getDownloadSize());
        tv_progress.setText(status.percent());
        Log.e("===============",status.percent());
        float f = Float.parseFloat(status.percent().substring(0,status.percent().length() - 1));
        f /= 100;
        Log.e("===============",f+"");
        RelativeLayout.LayoutParams lp= (RelativeLayout.LayoutParams) iv_progress.getLayoutParams();
        lp.leftMargin= (int) (progressBar.getWidth()*f);
        iv_progress.setLayoutParams(lp);
    }

    private void start() {
        RxDownload.INSTANCE.start(url).subscribe();
    }

    private void stop() {
        RxDownload.INSTANCE.stop(url).subscribe();
    }

    private void install() {
        RxDownload.INSTANCE.extension(url, ApkInstallExtension.class).subscribe();
    }

    private void open() {
        //TODO: open app
        File apkfile = new File("/storage/emulated/0/Download/","slxy_app.apk");
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");
        startActivity(i);
        android.os.Process.killProcess(android.os.Process.myPid());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dispose(disposable);
    }

    @Override
    public void onBackPressed() {

    }
}
